Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(chart): added environment variables for link service DB parameters in helm chart #90

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kiyo-s
Copy link

@kiyo-s kiyo-s commented Oct 19, 2024

Fixed php_network_getaddresses error that occurs when link service deployed with helm connects to database.

The detailed error is as follows.

kubectl logs unguard-like-service-98d7bb6fb-z8n5b -f
[2024-10-19 07:48:23] local.ERROR: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known {"exception":"[object] (PDOException(code: 2002): SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known at /var/www/app/Console/Commands/CreateDatabase.php:65)
[stacktrace]
#0 /var/www/app/Console/Commands/CreateDatabase.php(65): PDO->__construct('mysql:host=${UN...', 'root', 'sdfsgdkJO8LMcxK...')
#1 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\\Console\\Commands\\CreateDatabase->handle()
#2 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#3 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#4 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#5 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(610): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#6 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\\Container\\Container->call(Array)
#7 /var/www/vendor/symfony/console/Command/Command.php(298): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#8 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#9 /var/www/vendor/symfony/console/Application.php(1040): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#10 /var/www/vendor/symfony/console/Application.php(301): Symfony\\Component\\Console\\Application->doRunCommand(Object(App\\Console\\Commands\\CreateDatabase), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#11 /var/www/vendor/symfony/console/Application.php(171): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#12 /var/www/vendor/laravel/framework/src/Illuminate/Console/Application.php(93): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#13 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#14 /var/www/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#15 {main}

[previous exception] [object] (PDOException(code: 0): PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known at /var/www/app/Console/Commands/CreateDatabase.php:65)
[stacktrace]
#0 /var/www/app/Console/Commands/CreateDatabase.php(65): PDO->__construct('mysql:host=${UN...', 'root', 'sdfsgdkJO8LMcxK...')
#1 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\\Console\\Commands\\CreateDatabase->handle()
#2 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#3 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#4 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#5 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(610): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#6 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\\Container\\Container->call(Array)
#7 /var/www/vendor/symfony/console/Command/Command.php(298): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#8 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#9 /var/www/vendor/symfony/console/Application.php(1040): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#10 /var/www/vendor/symfony/console/Application.php(301): Symfony\\Component\\Console\\Application->doRunCommand(Object(App\\Console\\Commands\\CreateDatabase), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#11 /var/www/vendor/symfony/console/Application.php(171): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#12 /var/www/vendor/laravel/framework/src/Illuminate/Console/Application.php(93): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#13 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#14 /var/www/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#15 {main}
"} 

   PDOException 

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

  at app/Console/Commands/CreateDatabase.php:65
     61▕         $password = getenv('MARIADB_PASSWORD', false);
     62▕         $address = $host . ":" . $port;
     63▕         $dbName = getenv('DB_DATABASE', false);
     64▕ 
  ➜  65▕         $connection = new PDO("mysql:host=$address", $rootuser, $password);
     66▕         try {
     67▕             $connection->exec("CREATE DATABASE $dbName");
     68▕         } catch (PDOException $e) {
     69▕             Log::notice("PDO-Exception, probably due to database $dbName already existing", [$e]);

  1   app/Console/Commands/CreateDatabase.php:65
      PDO::__construct("mysql:host=${UNGUARD_MARIADB_SERVICE_HOST}:${UNGUARD_MARIADB_SERVICE_PORT_MYSQL}", "root", "sdfsgdkJO8LMcxK9492klhTtMB/TJ5si")

      +13 vendor frames 
  15  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Workaround:
I added env directly by the following command and it solved the problem.

kubectl edit deployments deployments.apps unguard-like-service

Adding only DB_HOST did not resolve the issue, so we also added DB_PORT.

@kiyo-s kiyo-s requested a review from a team as a code owner October 19, 2024 08:39
@dynatrace-cla-bot
Copy link
Member

dynatrace-cla-bot commented Oct 19, 2024

CLA assistant check
All committers have signed the CLA.

@W3D3
Copy link
Contributor

W3D3 commented Oct 21, 2024

Hi @kiyo-s !
First and foremost, thanks for the contribution!

Could you explain how you ran into this issue in a bit more detail? Did this just happen on a normal helm deploy using something like helm install unguard oci://ghcr.io/dynatrace-oss/unguard/chart/unguard --wait --namespace unguard --create-namespace?

Also please have a look at the commit guidelines outlined here and adapt your commit message to make commitlint pass :)

@kiyo-s kiyo-s force-pushed the bugfix/chart-link-service-env branch from 024eb6e to d79cfce Compare October 21, 2024 13:40
@kiyo-s kiyo-s changed the title added environment variables for link service DB parameters in helm chart 🐛 fix(chart): added environment variables for link service DB parameters in helm chart Oct 21, 2024
@kiyo-s
Copy link
Author

kiyo-s commented Oct 21, 2024

Hi, @W3D3 !
Thanks for the comment.
I've fixed the commit message according to the rules.

Could you explain how you ran into this issue in a bit more detail? Did this just happen on a normal helm deploy using something like helm install unguard oci://ghcr.io/dynatrace-oss/unguard/chart/unguard --wait --namespace unguard --create-namespace?

Yes, that's right.
I used helmfile, but I installed it with the default parameters.

In the end, I used the patch below to set DB_HOST and DB_PORT, and it worked as expected.
https://github.com/kiyo-s/eks-learn/blob/main/helm/unguard/helmfile.yaml#L32

Please check it.

@W3D3
Copy link
Contributor

W3D3 commented Oct 21, 2024

That's kind of interesting that using it this way leads to ${UNGUARD_MARIADB_SERVICE_HOST}:${UNGUARD_MARIADB_SERVICE_PORT_MYSQL} being evaluated as the db host in the like-service instead of the defaults. Honestly don't know where they get populated from if you run it with skaffold.
Never noticed it, as it works as expected when you just check out the unguard repo and execute skaffold run.

But either way, I like your suggestion as in making these values explicitly passed through the values file / k8s manifests, as we should not rely on skaffold.

One small issue with your PR:

If you specificy the DB_HOST in the values.yaml as a helm template string, you also have to use the tpl function to resolve the template string in the actualy Kubernetes manifest (like-service.yaml)

DB_HOST: "{{ .Values.mariaDB.serviceName }}"

Like this:

             - name: USER_AUTH_SERVICE_ADDRESS
               value: {{ quote .Values.likeService.deployment.container.env.USER_AUTH_SERVICE_ADDRESS }}
             - name: DB_HOST
-              value: {{ quote .Values.likeService.deployment.container.env.DB_HOST }}
+              value: {{ tpl .Values.likeService.deployment.container.env.DB_HOST . }}
             - name: DB_PORT
               value: {{ quote .Values.likeService.deployment.container.env.DB_PORT }}
             - name: MARIADB_PASSWORD

Copy link
Contributor

@W3D3 W3D3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment.

@kiyo-s
Copy link
Author

kiyo-s commented Oct 22, 2024

Thanks for the comment.
I'll fix it later.

@kiyo-s kiyo-s force-pushed the bugfix/chart-link-service-env branch from 233a2e4 to a491481 Compare October 27, 2024 01:34
@kiyo-s
Copy link
Author

kiyo-s commented Oct 27, 2024

@W3D3
I'm sorry for the delay in responding.
I fixed the errors you pointed out.
Please check.

@kiyo-s kiyo-s force-pushed the bugfix/chart-link-service-env branch from a491481 to 5c8b6d3 Compare October 27, 2024 13:25
@kiyo-s
Copy link
Author

kiyo-s commented Oct 27, 2024

Sorry, there was a mistake in the commit message and helm chart, so I fixed it.

To allow reading of another value
@kiyo-s kiyo-s force-pushed the bugfix/chart-link-service-env branch from 5c8b6d3 to 6787a76 Compare October 27, 2024 13:58
@W3D3
Copy link
Contributor

W3D3 commented Oct 30, 2024

Looks good! Please rebase on latest main and then we can merge this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants